package src;
/**
 * The VariableExpression class, in which implements Variable and Expression Interfaces
 * represents a variable expression.
 * A variable expression can be evaluated, derived and return its last operation symbol.
 * A variable expression has a value that can be set and a name.
 *
 * @author Intro111 Staff
 */

public class VariableExpression implements Variable, Expression {

    private char name;
    private double value;

    /**
     * Constructor that creates a variable expression
     *
     * @param name  the name of the variable
     * @param value the value of the variable
     */
    public VariableExpression(char name, double value) {
        this.name = name;
        this.value = value;
    }

    /**
     * Constructor that creates a variable expression with value 0
     *
     * @param name the name of the variable expression
     */

    public VariableExpression(char name) {
        this(name, 0);
    }

    /**
     * Get the value of the variable
     *
     * @return the value of the variable
     */

    public double getValue() {
        return value;
    }

    /**
     * Sets the value of the variable
     *
     * @param value the value to set
     */
    public void setValue(double value) {
        this.value = value;
    }

    /**
     * Get the char symbol that represents of the variable
     *
     * @return char that represents the variable
     */
    public char getName() {
        return name;
    }

    /**
     * Evaluate variable expression and return the result
     *
     * @return double that represents the evaluation result of the variable expression
     */
    public double evaluate() {
        return value;
    }


    /**
     * Get the last operation symbol of Variable Expression, which is simply the empty String "".
     *
     * @return String empty String ""
     */
    public String getLastOperationSymbol() {
        return "";
    }

    /**
     * Derives the Variable expression by a variable.
     *
     * @param var the variable to derive by
     * @return Constant Expression with value 0 or 1 (depends if param var equals to the variable expression)
     * @throws RunTimeException for a null var
     */
    public Expression derivative(Variable var) {
        if (var == null) {
            throw new RuntimeException("VariableExpression derivative(Variable var) got a null argument.");
        }

        if (this.equals(var))
            return new Constant(1);
        else
            return new Constant(0);
    }

    /**
     * Compares this expression to the specified object.
     * The result is true if and only if the argument is exactly the same Expression (same reference) as the current variable expression
     *
     * @param other The object to compare this expression against
     * @return true if the given object represents an expression equivalent to this expression, false otherwise
     */
    public boolean equals(Object other) {
        return (this == other);
    }

    /**
     * Returns a string representing the variable expression.
     *
     * @return String that represents the variable expression
     */
    public String toString() {
        return "" + name;
    }


}
